package com.qttx.myapplication;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;

import com.qttx.myapplication.base.BaseConstant;
import com.qttx.myapplication.fragment.FourFragment;
import com.qttx.myapplication.fragment.OneFragment;
import com.qttx.myapplication.fragment.ThreeFragment;
import com.qttx.myapplication.fragment.TwoFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.fl_main_homepage)
    FrameLayout flMainHomepage;
    @Bind(R.id.rb_main_one)
    RadioButton rbMainOne;
    @Bind(R.id.rb_main_two)
    RadioButton rbMainTwo;
    @Bind(R.id.rb_main_three)
    RadioButton rbMainThree;
    @Bind(R.id.rb_main_four)
    RadioButton rbMainFour;
    private OneFragment oneFragment;
    private TwoFragment twoFragment;
    private ThreeFragment threeFragment;
    private FourFragment fourFragment;
    private FragmentManager supportFragmentManager;
    private FragmentTransaction fragmentTransaction;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("TAG", "MainActivity onCreate()savedInstanceState============" + savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        switchToFragment(BaseConstant.RB_ONE);
    }


    @OnClick({R.id.rb_main_one, R.id.rb_main_two, R.id.rb_main_three, R.id.rb_main_four})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rb_main_one:
                switchToFragment(BaseConstant.RB_ONE);
                break;
            case R.id.rb_main_two:
                switchToFragment(BaseConstant.RB_TWO);
                break;
            case R.id.rb_main_three:
                switchToFragment(BaseConstant.RB_THREE);
                break;
            case R.id.rb_main_four:
                switchToFragment(BaseConstant.RB_FOUR);
                break;
        }
    }


    private void switchToFragment(int i) {
        //点击加载不同的布局，获取fragmentManager并且开启事务,如果是全局的fragmentTransaction 只能comit一次(为什么要放在这个地方是为了一开始便加载推荐页面)
        supportFragmentManager = getSupportFragmentManager();
        fragmentTransaction = supportFragmentManager.beginTransaction();
        //在显示之前需要先隐藏各个fragment
        hidAllFragment();
        //跳转到不同的fragment
        switch (i) {
            case BaseConstant.RB_ONE:
                if (oneFragment == null) {
                    oneFragment = new OneFragment();
                    fragmentTransaction.add(R.id.fl_main_homepage, oneFragment);
                } else {
                    fragmentTransaction.show(oneFragment);
                }

                break;
            case BaseConstant.RB_TWO:
                if (twoFragment == null) {
                    twoFragment = new TwoFragment();
                    fragmentTransaction.add(R.id.fl_main_homepage, twoFragment);
                } else {
                    fragmentTransaction.show(twoFragment);
                }
                break;
            case BaseConstant.RB_THREE:
                if (threeFragment == null) {
                    threeFragment = new ThreeFragment();
                    fragmentTransaction.add(R.id.fl_main_homepage, threeFragment);
                } else {
                    fragmentTransaction.show(threeFragment);
                }
                break;
            case BaseConstant.RB_FOUR:

                if (fourFragment == null) {
                    fourFragment = new FourFragment();
                    fragmentTransaction.add(R.id.fl_main_homepage, fourFragment);
                } else {
                    fragmentTransaction.show(fourFragment);
                }
                break;
        }
        fragmentTransaction.commit();
    }

    private void hidAllFragment() {
        if (oneFragment != null) {
            fragmentTransaction.hide(oneFragment);
        }
        if (twoFragment != null) {
            fragmentTransaction.hide(twoFragment);
        }
        if (threeFragment != null) {
            fragmentTransaction.hide(threeFragment);
        }
        if (fourFragment != null) {
            fragmentTransaction.hide(fourFragment);
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }


}
